package com.bw.play.repo

import com.bw.common.entity.CommendEntity
import com.bw.common.entity.DanmuEntity
import com.bw.common.entity.MovieVideoEntity
import com.bw.common.entity.SimpleVideoEntity
import com.bw.common.repo.BaseRepository
import com.bw.play.api.PlayApiServer
import com.bw.play.entity.AuthorEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import okhttp3.RequestBody
import javax.inject.Inject

class PlayVideoRepository @Inject constructor(private val apiServer: PlayApiServer) :
    BaseRepository() {

    suspend fun getAuthorById(userId: String): Flow<AuthorEntity?> {
        return flow {
            val result = apiServer.getAuthorById(userId)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit(null)
            }
        }
    }

    //获取首页推荐视频
    suspend fun getRecommendSimpleVideo(page: Int, pageSize: Int): Flow<List<SimpleVideoEntity>> {
        return flow {
            val result = apiServer.getRecommendSimpleVideo(page, pageSize)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit(emptyList())
            }
        }
    }

    //获取首页分类下视频
    suspend fun getSimpleVideoByChannelId(
        channelId: String,
        page: Int,
        pageSize: Int
    ): Flow<List<SimpleVideoEntity>> {
        return flow {
            val result = apiServer.getSimpleVideoByChannelId(channelId, page, pageSize)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit(emptyList())
            }
        }
    }

    //获取放映厅推荐视频
    suspend fun getRecommendMultiVideo(page: Int, pageSize: Int): Flow<List<MovieVideoEntity>> {
        return flow {
            val result = apiServer.getRecommendMultiVideo(page, pageSize)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit(emptyList())
            }
        }
    }

    //获取放映厅分类下视频
    suspend fun getMultiVideoByTypeId(
        typeId: Int,
        page: Int,
        pageSize: Int
    ): Flow<List<MovieVideoEntity>> {
        return flow {
            val result = apiServer.getMultiVideoByTypeId(typeId, page, pageSize)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit(emptyList())
            }
        }
    }

    //获取视频评论
    suspend fun getCommentByUserId(dataType: Int, itemId: String): Flow<List<CommendEntity>> {
        return flow {
            val result = apiServer.getCommentByUserId(dataType, itemId)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit(emptyList())
            }
        }
    }

    //发送评论
    suspend fun publishComment(body: RequestBody): Flow<CommendEntity?> {
        return flow {
            val result = apiServer.publishComment(body)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit(null)
            }
        }
    }

    //发布弹幕
    suspend fun publishDanmuMsg(requestBody: RequestBody): Flow<String> {
        return flow {
            val result = apiServer.publishDanmuMsg(requestBody)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit("")
            }
        }
    }

    //获取视频弹幕
    suspend fun getVideoDanmuMsg(dataType: Int, itemId: String): Flow<List<DanmuEntity>> {
        return flow {
            val result = apiServer.getVideoDanmuMsg(dataType, itemId)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit(emptyList())
            }
        }
    }


}